{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b16227a7-d8ca-462f-9926-9649342ba0d2",
   "metadata": {},
   "source": [
    "# Negation\n",
    "\n",
    "The negation operation receives a quantum register representing some number $x$\n",
    "and returns a quantum register containing $-x$. Integer and fixed point numbers are both supported."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c3e4ae57-2b9a-4318-bd50-6f9651b5109e",
   "metadata": {},
   "source": [
    "## Example\n",
    "\n",
    "The following example will show negation of a signed quantum variable."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "1ef4da37-e2c0-43e7-b525-7a2f001788a6",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T13:21:46.407142Z",
     "iopub.status.busy": "2024-05-07T13:21:46.405729Z",
     "iopub.status.idle": "2024-05-07T13:21:49.547451Z",
     "shell.execute_reply": "2024-05-07T13:21:49.546785Z"
    }
   },
   "outputs": [],
   "source": [
    "from classiq import (\n",
    "    Output,\n",
    "    QArray,\n",
    "    QBit,\n",
    "    QNum,\n",
    "    allocate_num,\n",
    "    create_model,\n",
    "    hadamard_transform,\n",
    "    qfunc,\n",
    ")\n",
    "\n",
    "\n",
    "@qfunc\n",
    "def main(a: Output[QNum], b: Output[QNum]) -> None:\n",
    "    allocate_num(3, True, 0, a)\n",
    "    hadamard_transform(a)\n",
    "    b |= -a\n",
    "\n",
    "\n",
    "qmod = create_model(main)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "0cc7105a-ec20-48f6-80d3-9d6a969581ab",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T13:21:49.550710Z",
     "iopub.status.busy": "2024-05-07T13:21:49.550063Z",
     "iopub.status.idle": "2024-05-07T13:21:52.271907Z",
     "shell.execute_reply": "2024-05-07T13:21:52.271171Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'a': 0.0, 'b': 0.0}: 143,\n",
       " {'a': -2.0, 'b': 2.0}: 136,\n",
       " {'a': -1.0, 'b': 1.0}: 130,\n",
       " {'a': 1.0, 'b': -1.0}: 122,\n",
       " {'a': 2.0, 'b': -2.0}: 121,\n",
       " {'a': 3.0, 'b': -3.0}: 120,\n",
       " {'a': -4.0, 'b': 4.0}: 114,\n",
       " {'a': -3.0, 'b': 3.0}: 114]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from classiq import execute, synthesize, write_qmod\n",
    "\n",
    "write_qmod(qmod, \"negation_example\")\n",
    "qprog = synthesize(qmod)\n",
    "\n",
    "result = execute(qprog).result()[0].value\n",
    "result.parsed_counts"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
